package com.chance.cc.crawler.development.command.job.domain.weibo.api.realtime;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.domain.weibo.WeiboCommonScript;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.turnPageItem;

/**
 * @author lt
 * @version 1.0
 * @date 2021-03-17 11:09:49
 * @email okprog@sina.com
 */
public class WeiboKeywordsRealtimeCrawlerSchedulerJob extends WeiboCommonScript {
    private static final String crawler_level = "normal";
    public static final String siteBiz = "article-realtime";

    public static void main(String[] args) {
//        publishCrawlerScheduleJobInfo();
        System.out.println(crawlerJobScheduler().generateCrawlerTriggerKey());
    }
    /**
     * 发布任务信息
     * @return
     */
    public static CrawlerJob publishCrawlerScheduleJobInfo(){

        CrawlerJob dxyScheduler = crawlerJobScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(dxyScheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("微博API根据关键词两小时实时采集任务");
        crawlerScheduleJob.setCrawlerKey(dxyScheduler.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return dxyScheduler;
    }

    public static CrawlerJob crawlerJobScheduler(){

        //文章采集
        CrawlerRequestRecord searchKWRecord = searchKWRecord();

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",domain, kwSite, crawler_level, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-", "crawler", domain, kwSite, crawler_level, "queue")))
                .kafkaResultPipeline(null, kafkaTopicForRealTimeJob, null)
                .fileResultPipeline(null, "/data/chance_crawler_test/data/weibo/weibo_medical_keyword_realtime.log", false)
                .crawlerJobThreadNumber(3)
                .requestRecord(searchKWRecord)
                .supportRecord(getKwRecord(kwSite))
                .build();
        //配置采集输出
        crawlerJob.getScheduleTags().getCategoryTag().addKVTag(IS_OUTPUT_Age,true);
        crawlerJob.getScheduleTags().getCategoryTag().addKVTag(IS_OUTPUT_Tags,true);
//        crawlerJob.getScheduleTags().getCategoryTag().addKVTag(IS_OUTPUT_Interaction,true);
        return crawlerJob;
    }

    /**
     * 初始record
     * @return
     */
    public static CrawlerRequestRecord searchKWRecord(){
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, turnPageItem)
                .httpUrl("https://www.weibo.com")
                .releaseTime(System.currentTimeMillis())
                .filter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(2,null))
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,kwSite,"queue")))
                .needWashed(false)
                .needParsed(false)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);
        return requestRecord;
    }
}
